
#include "dproperty.h"
#include "property.h"
#include "dmp.h"
#include "stdio.h"
#include "stdlib.h"
#include "acnstdtypes.h"

#define AUTO_CREATE 1
#define PROP_SIZE 2
#define RADOM_CREAT_NUM 5

void process_create(){
	dmp_property_map_t *prop_new = NULL;
	uint16_t size = PROP_SIZE;
	uint32_t addr = 2;
	printf("please input the addr for new property \n");
	scanf("%d",&addr);
	if((prop_new = property_new(addr,size))==NULL){
		printf("create property error \n");
		return;
	}else{
		printf("create property success \n");
	}
	display_property(prop_new);
}

void process_delete(){
	printf("please input the addr of property \n");
	uint32_t addr;
	scanf("%d",&addr);
	if(property_deleteby_address(addr)==FAIL){
		printf("delete property error \n");
		return;
	}else{
		printf("delete property success \n");
	}
}

void process_displayAll(){
	display_all();
}

void process_set_value(){
	printf("please input the addr of property \n");
	uint32_t addr;
	uint16_t size = PROP_SIZE;
	dmp_property_map_t *prop = NULL;
	uint8_t *ptr = (uint8_t *)malloc(sizeof(uint8_t)*PROP_SIZE);

	int i ;//for loop
	scanf("%d",&addr);
	if((prop = property_findby_address(addr))==NULL){
		prop = property_new(addr,size);
	}
	for(i=0;i<PROP_SIZE;i++){
		ptr[i] = (i+1) * rand();
		printf("set value ptr[%d] value %d \n",i,ptr[i]);
	}
	set_property(addr,ptr);
	free(ptr);
	display_property(prop);

}

void process_get_property(){

	uint16_t size;
	uint32_t addr;
	int i ;
	printf("please input the addr of property \n");
	scanf("%d",&addr);
	uint8_t *ptr = NULL;
	size = get_property_ptr(addr,&ptr);
	for(i=0;i<size;i++){
		printf("set value ptr[%d] value %d \n",i,ptr[i]);
	}
}

void process_closeAll(){
	property_close();
}

void dirver_property()
{

	printf("Now we are in the test driver for property \n");
	bool exit = false;
	int i = 0;//for loop
	int j = 0;
#ifdef AUTO_CREATE
	for(i=0;i<RADOM_CREAT_NUM;i++){
		uint16_t size = PROP_SIZE;
		uint32_t addr = rand();
		uint8_t *ptr = (uint8_t *)malloc(sizeof(uint8_t)*size);
		if(property_new(addr,size)==NULL){
			printf("create property error \n");
			return;
		}else{
			printf("create property success \n");
		}

		for(j=0;j<size;j++){
			ptr[j] = (j+1) * rand();
		}
		set_property(addr,ptr);
		free(ptr);
	}
#endif
	while(!exit){
		switch(mygetch()){
		case 'a':case 'A':
			process_displayAll();
			break;
		case 'c':
			process_create();
			break;
		case 'd':case 'D':
			process_delete();
			break;
		case 'e':
			process_closeAll();
			break;
		case 'g':
			process_get_property();
			break;
		case 's':
			process_set_value();
			break;
		case 'x' : case 'X':
			exit = true;
			break;
		}
	}//while
}//function

